{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a5cabe5a-e21f-4f46-90db-41c39c23b794",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:00.653629500Z",
     "start_time": "2024-01-10T06:03:58.105699500Z"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "eff81470-e753-4528-813a-3b5470b5fb11",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:34.741050900Z",
     "start_time": "2024-01-10T06:04:34.670091800Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<tf.Tensor: shape=(), dtype=int32, numpy=2>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.constant(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d8007e6d-a39b-467c-8d01-452556897ca2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:35.989370300Z",
     "start_time": "2024-01-10T06:04:35.945380Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<tf.Tensor: shape=(5,), dtype=float32, numpy=array([1., 2., 3., 4., 5.], dtype=float32)>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.constant([1,2,3,4,5.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "92d7ab60-a0e3-4575-9294-d47b2a739768",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:36.750377300Z",
     "start_time": "2024-01-10T06:04:36.719398500Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<tf.Tensor: shape=(2, 2), dtype=int32, numpy=\narray([[1, 2],\n       [3, 4]])>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.constant([[1,2],[3,4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8fa97d13-706d-490c-8945-745831ce8d36",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:37.414478300Z",
     "start_time": "2024-01-10T06:04:37.374484900Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tv1 = tf.Variable(3)\n",
    "tv1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "79c7ee3b-0065-4ac8-babe-5ff0fea30af9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:38.042975600Z",
     "start_time": "2024-01-10T06:04:37.995999500Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<tf.Variable 'number:0' shape=() dtype=int32, numpy=3>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = tf.Variable(3, name=\"number\")\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "36655b74-e795-4934-8c8b-88eacbb213ea",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:38.702034300Z",
     "start_time": "2024-01-10T06:04:38.687040200Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor([4 6], shape=(2,), dtype=int32)\n",
      "[4 6]\n"
     ]
    }
   ],
   "source": [
    "# 加\n",
    "t1 = tf.constant([3,4])\n",
    "t2 = tf.constant([1,2])\n",
    "result = tf.add(t1,t2)\n",
    "print(result)\n",
    "print(result.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "28a6acca-625b-4d88-b211-39e8e4ed2567",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:39.949544900Z",
     "start_time": "2024-01-10T06:04:39.933555200Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(2, shape=(), dtype=int32)\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 减\n",
    "a = tf.constant(5)\n",
    "b = tf.constant(3)\n",
    "c = tf.subtract(a, b)\n",
    "print(c)\n",
    "print(c.numpy()) # 输出结果为2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "907328c2-9c52-4913-b4ad-80594a48f848",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:40.903786600Z",
     "start_time": "2024-01-10T06:04:40.879803700Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[3]\n",
      " [8]], shape=(2, 1), dtype=int32)\n",
      "[[3]\n",
      " [8]]\n",
      "------------------------------\n",
      "tf.Tensor(\n",
      "[[ 5  6]\n",
      " [13 16]], shape=(2, 2), dtype=int32)\n",
      "[[ 5  6]\n",
      " [13 16]]\n"
     ]
    }
   ],
   "source": [
    "# 乘\n",
    "a = tf.constant([[1], [2]])\n",
    "b = tf.constant([[3], [4]])\n",
    "c = tf.multiply(a, b) #计算张量的乘法\n",
    "print(c)\n",
    "print(c.numpy()) # 输出结果为[[10] [8]]\n",
    "print(\"---\"*10)\n",
    "a1 = tf.constant([[1,2],[3,4]])\n",
    "b1 = tf.constant([[3,4],[1,1]])\n",
    "d = tf.matmul(a1,b1) # 计算乘法\n",
    "print(d)\n",
    "print(d.numpy())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "aae4b98e-e416-44e2-9b1a-6bc3308a48f9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:41.651201600Z",
     "start_time": "2024-01-10T06:04:41.628218300Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(3.0, shape=(), dtype=float64)\n",
      "3.0\n"
     ]
    }
   ],
   "source": [
    "# 除\n",
    "a = tf.constant(6)\n",
    "b = tf.constant(2)\n",
    "c = tf.divide(a, b)\n",
    "print(c)\n",
    "print(c.numpy()) # 输出结果为3.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0f5624a2-3bb0-4051-a6c3-09dee47cc44a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:46.354815Z",
     "start_time": "2024-01-10T06:04:46.308271800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[ 0.5114028  -1.7071493   0.42661032]\n",
      " [-0.06941243  0.54363453 -0.8834552 ]], shape=(2, 3), dtype=float32)\n",
      "[1 2 3]\n",
      "将张量转换为numpy数组\n",
      "[[ 0.5114028  -1.7071493   0.42661032]\n",
      " [-0.06941243  0.54363453 -0.8834552 ]]\n",
      "numpy到tensor的转换\n",
      "tf.Tensor(\n",
      "[[5 6]\n",
      " [7 8]], shape=(2, 2), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "# 定义一个随机生成2x3矩阵的张量\n",
    "tensor = tf.random.normal([2,3])\n",
    "print(tensor)\n",
    "# 将1x3的列表转为一维的numpy数组\n",
    "list_a = [1, 2, 3]\n",
    "arr_a = np.array(list_a)\n",
    "print(arr_a)\n",
    "\n",
    "# 将张量转换为numpy数组\n",
    "arr_b = tensor.numpy()\n",
    "print(\"将张量转换为numpy数组\")\n",
    "print(arr_b)\n",
    "\n",
    "arr_e = np.array([[5,6],[7,8]])\n",
    "tensor_e = tf.constant(arr_e)\n",
    "print(\"numpy到tensor的转换\")\n",
    "print(tensor_e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3fea1685-fce6-4601-aa37-621555d54e3a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-10T06:04:48.273500400Z",
     "start_time": "2024-01-10T06:04:48.241497500Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(20.0, shape=(), dtype=float32)\n",
      "tf.Tensor(5.0, shape=(), dtype=float32)\n",
      "tf.Tensor(10.0, shape=(), dtype=float32)\n",
      "tf.Tensor(1.0, shape=(), dtype=float32)\n",
      "[1 0]\n",
      "[0 1]\n"
     ]
    }
   ],
   "source": [
    "t = tf.constant([[4.0,5.0],[10.0,1.0]])\n",
    "\n",
    "print(tf.reduce_sum(t))\n",
    "print(tf.reduce_mean(t))\n",
    "print(tf.reduce_max(t))\n",
    "print(tf.reduce_min(t))\n",
    "print(tf.argmax(t).numpy()) #最大值的索引\n",
    "print(tf.argmin(t).numpy()) #最小值的索引\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c3987da-5ea0-4664-9c9c-92d175b71490",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8096c96f-0b1d-438b-be60-22dcb5f66c5b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
